import 'dart:math';
import 'package:charts_flutter/flutter.dart';
import 'package:dio/dio.dart';
import 'package:epidemic_data/data/data.dart';
import 'package:epidemic_data/model/city_info_response.dart';
import 'dart:io';

import 'package:flutter/material.dart';

import 'package:charts_flutter/flutter.dart' as charts;

class LineChartWidget extends StatefulWidget {


  @override
  _LineChartWidgetState createState() {
    return _LineChartWidgetState();
  }
}

class _LineChartWidgetState extends State<LineChartWidget> {

  var random = Random();

  List<TimeSeriesChart> data= [];


  static List<Trend> trendList = [];
  static List<Trend> sublist = [];
  static List<charts.Series> seriesList =[];
  var axis = charts.NumericAxisSpec(
      renderSpec: charts.GridlineRendererSpec(
        labelStyle: charts.TextStyleSpec(
            fontSize: 10,
            color: charts.MaterialPalette
                .white), //chnage white color as per your requirement.
      ));

  @override
  void initState() {
    super.initState();
    _getTrendList();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        height: 200,
        child: charts.TimeSeriesChart(_createSampleData(),
            animate: true,
            primaryMeasureAxis: axis,
            secondaryMeasureAxis: axis,
            defaultRenderer: charts.LineRendererConfig(
              // 圆点大小
              radiusPx: 5.0,
              stacked: false,
              // 线的宽度
              strokeWidthPx: 2.0,
              // 是否显示线
              includeLine: true,
              // 是否显示圆点
              includePoints: true,
              // 是否显示包含区域
              includeArea: true,
              // 区域颜色透明度 0.0-1.0
              areaOpacity: 0.2,
            ))
    );
  }

  void _getTrendList() async {
    // Options options = Options(headers: {HttpHeaders.authorizationHeader :"APPCODE fc126498c1af4ac4a325a96229286f78"});
    // Dio dio = Dio();
    // final response = await dio.get(
    //     "http://ncovdata.market.alicloudapi.com/ncov/cityDiseaseInfoWithTrend",
    //     options: options);
    // CityInfoResponse cityInfo = CityInfoResponse.fromJson(response.data);
    CityInfoResponse cityInfo = CityInfoResponse.fromJson(cityInfoFake);
    trendList = cityInfo.trend;
    sublist = trendList.sublist(trendList.length-8,trendList.length);
  }

  static List<charts.Series<TimeSeriesSales, DateTime>> _createSampleData() {
    List<TimeSeriesSales> data=[];
    for (int i = 0;i<sublist.length-1;i++){
      data.add(TimeSeriesSales( new DateTime(sublist[i+1].fullDay~/10000, sublist[i+1].fullDay~/100%100, sublist[i+1].fullDay%100), sublist[i+1].sureCnt-sublist[i].sureCnt));
    }
    return [
      new charts.Series<TimeSeriesSales, DateTime>(
        id: 'Sales',
        colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
        domainFn: (TimeSeriesSales sales, _) => sales.time,
        measureFn: (TimeSeriesSales sales, _) => sales.sales,
        data: data,
      )
    ];
  }
}

class TimeSeriesSales {
  final DateTime time;
  final int sales;

  TimeSeriesSales(this.time, this.sales);
}

